package com.wxtt.android.recyclerview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.wxtt.android.R;

/**
 * 说明：RecylerView分割线
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/1/25 22:44
 * <p/>
 * 版本：verson 1.0
 */
public class DriverItemDecoration extends RecyclerView.ItemDecoration {

    private Context mContext;
    private Drawable mDrawable;

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        this.mContext = parent.getContext();
        this.mDrawable = mContext.getResources().getDrawable(R.drawable.divider);
        drawVertical(c,parent);
    }

    private void drawVertical(Canvas c,RecyclerView parent){

        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        int childCount = parent.getChildCount();
        for (int i = 0;i<childCount;i++){
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + mDrawable.getIntrinsicHeight();
            mDrawable.setBounds(left, top, right, bottom);
            mDrawable.draw(c);
        }
    }
}
